package afu.project.core.filter;

import afu.project.core.context.GatewayContext;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @description 过滤器链 用于存储实现的过滤器的信息 并且按照顺序进行执行
 */
@Slf4j
public class GatewayFilterChain {

    private List<Filter> filters = new ArrayList<>();


    public GatewayFilterChain addFilter(Filter filter) {
        filters.add(filter);
        return this;
    }

    public GatewayFilterChain addFilterList(List<Filter> filter) {
        filters.addAll(filter);
        return this;
    }


    /**
     * 执行过滤器处理流程
     *
     * @param ctx
     * @return
     * @throws Exception
     */
    public GatewayContext doFilter(GatewayContext ctx) {
        if (filters.isEmpty()) {
            return ctx;
        }
        try {
            for (Filter fl : filters) {
                fl.doFilter(ctx);
            }
        } catch (Exception e) {
            log.error("执行过滤器发生异常：{}", e.getMessage());
            throw new RuntimeException(e);
        }
        return ctx;
    }
}